package com.selfdriving.adapter;

import java.util.List;
import com.selfdriving.R;
import com.selfdriving.bean.Feature;
import com.selfdriving.utils.StringUtil;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TravelAdapter extends BaseAdapter {

	private Context context;

	private List<Feature> features;

	private static final int TIME_DATE = 0x1;

	private static final int TIME_PICTURE = 0x2;

	private static final int TIME_ERROR = 0x3;

	private HolderView holderView;

	public TravelAdapter(Context context, List<Feature> features) {
		this.context = context;
		this.features = features;
	}

	@Override
	public int getCount() {
		return features.size();
	}

	@Override
	public Object getItem(int arg0) {
		return features.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		if (features.get(position).flag == 0) {
			return TIME_DATE;
		} else {
			return TIME_PICTURE;
		}
	}

	@Override
	public int getViewTypeCount() {
		return TIME_ERROR;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Feature feature = features.get(position);
		int type = getItemViewType(position);
		if (convertView == null) {
			holderView = new HolderView();
			switch (type) {
			case TIME_DATE:
				convertView = ((LayoutInflater) context
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
						.inflate(R.layout.strategy_item_one, null);
				
				break;
			case TIME_PICTURE:
				convertView = ((LayoutInflater) context
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
						.inflate(R.layout.travel_item, null);
				break;
			}
			convertView.setTag(holderView);
		} else {
			holderView = (HolderView) convertView.getTag();
		}
		
		if (type == TIME_DATE) {
			holderView.time = (TextView) convertView.findViewById(R.id.tvTime);
			if (!StringUtil.isNullOrEmpty(feature.time)) {
				holderView.time.setText(feature.time);
			} else {
				holderView.time.setVisibility(View.GONE);
			}
		}
		return convertView;
	}

	private class HolderView {

		private ImageView icon;

		private ImageView comment;

		private TextView time;

	}

}
